//
//  SecretdetailViewController.m
//  Secret
//
//  Created by 隋文涛 on 13-12-11.
//  Copyright (c) 2013年 隋文涛. All rights reserved.
//

#import "SecretdetailViewController.h"

@interface SecretdetailViewController ()

@end

@implementation SecretdetailViewController
@synthesize WriteMode;
@synthesize path;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        WriteMode = NO;
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    if (_Switch_ReadMode.isOn) {
        stringdata =  _TextView_Read.text;
        NSData *data = [stringdata dataUsingEncoding:NSUTF8StringEncoding];
        [data writeToFile:path atomically:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [_TextView_Read setEditable:NO];
    if (WriteMode == YES) {
        [_Switch_ReadMode setOn:YES];
        [self ReadModeClicked:_Switch_ReadMode];
    }
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    stringdata = str;
//    stringdata = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    _TextView_Read.text = stringdata;
    
}

#pragma mark keyboard notification

- (void)keyboardWillShow:(NSNotification *) notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    
    NSLog(@"height:%f",height);
//    CGRect keyboardBounds;
//    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
//    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
//    NSLog(@"keyboardBounds.size.height:%f",keyboardBounds.size.height);
//    NSLog(@"duration:%@",duration);
//    NSLog(@"curve:%@",curve);
    
    
    
    CGRect bottomBarFrame = self.TextView_Read.frame;
    
        [UIView beginAnimations:@"bottomBarUp" context:nil];
        [UIView setAnimationDuration: animationDuration];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        bottomBarFrame.size.height = self.view.frame.size.height - height;
        self.TextView_Read.frame = bottomBarFrame;
        [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *) notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    
    CGRect bottomBarFrame = self.TextView_Read.frame;

        [UIView beginAnimations:@"bottomBarDown" context:nil];
        [UIView setAnimationDuration: animationDuration];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        bottomBarFrame.size.height = self.view.frame.size.height;
        self.TextView_Read.frame = bottomBarFrame;
        [UIView commitAnimations];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)ReadModeClicked:(id)sender {
    if (((UISwitch *)sender).isOn) {
        [_Label_Mode setText:@"编辑"];
        [_TextView_Read setEditable:YES];
        [_TextView_Read becomeFirstResponder];
        
    }else{
        [_Label_Mode setText:@"浏览"];
        [_TextView_Read resignFirstResponder];
        [_TextView_Read setEditable:NO];
        //......
        stringdata =  _TextView_Read.text;
        NSData *data = [stringdata dataUsingEncoding:NSUTF8StringEncoding];
        [data writeToFile:path atomically:YES];
//        [stringdata writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
}
@end
